package zhcms.framework.dao;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.ResultSet;
import java.sql.SQLException;
import zhcms.db.DBManager;
import zhcms.db.ConnectionPool.PooledConnection;
import zhcms.framework.interfacepage.GyFrontAnalyze;

public class GyPlusParse {

	private String PlusName="";
	
	public String getPlusName() {
		return PlusName;
	}

	public void setPlusName(String plusName) {
		PlusName = plusName;
	}

	private String PlusOtherName="";
	
	public String getPlusOtherName() {
		return PlusOtherName;
	}

	public void setPlusOtherName(String plusOtherName) {
		PlusOtherName = plusOtherName;
	}
	
	private String PlusReturnS="";

	public String getPlusReturnS() {
		return PlusReturnS;
	}

	public void setPlusReturnS(String plusReturnS) {
		PlusReturnS = plusReturnS;
	}

	public GyPlusParse() {
		this.setPlusReturnS("");
	}
	
	
	@SuppressWarnings({ "unused" })
	public void Parse()
	{
		ResultSet rs = null;
		PooledConnection conn = null;
		String sql="select `id` , `name` , `jarclasstruepath` , `jarurl` , `classname` , `isbaohan`  from `gyhy_plus` where isok='1' and `name`='"+this.PlusName+"'";
		//System.out.println(sql);
		try {
			conn = DBManager.getConnection();
			rs = conn.executeQuery(sql);
			if (rs.next())
			{
				String id=rs.getString("id");
				String jarclasstruepath=rs.getString("jarclasstruepath");
				String jarurl=rs.getString("jarurl");
				String jarclassname=rs.getString("classname");
				String isbaohan=rs.getString("isbaohan");
				
				//System.out.println("id++++"+id);
				//System.out.println("jarclasstruepath++++"+jarclasstruepath);
				//System.out.println("jarurl++++"+jarurl);
				//System.out.println("jarclassname++++"+jarclassname);
				//System.out.println("isbaohan++++"+isbaohan);
				//String baohanjsp=rs.getString("baohanjsp");
				//String htmlurl=rs.getString("htmlurl");
				if(isbaohan.equals("1"))
				{
					//通过反射得到这个插件的内容
					//动态加载jar包
					File f=new File(jarclasstruepath+"/"+jarurl);
					if(!(f.exists())){
					  // System.out.println("jar file :"+jarclasstruepath+"/"+jarurl+" not exist!");
					   //this.setPlusReturnS("该插件不存在");
					   this.setPlusReturnS("");
					}
					else
					{
						try {
							URL url = new URL("file:"+jarclasstruepath+"/"+jarurl);
							//加载jar包
							URLClassLoader loader = new URLClassLoader( new URL[]{ url } );
							
							GyFrontAnalyze action;
							try {
								action = (GyFrontAnalyze) Class.forName(jarclassname).newInstance();
								Object obj = Class.forName(jarclassname).newInstance();  
								
								try {
									Method method = action.getClass().getMethod("getHtml", new Class[] {String.class});
									try {
									//System.out.println("method++++"+t_methodname.toString());
									String actionForward = (String) method.invoke(obj, this.PlusOtherName);
									//System.out.println("ReIsOk++++"+actionForward);
									this.setPlusReturnS(actionForward);
										
									} catch (IllegalArgumentException e) {

										e.printStackTrace();
									} catch (InvocationTargetException e) {

										e.printStackTrace();
									}
								} catch (SecurityException e) {

									e.printStackTrace();
								} catch (NoSuchMethodException e) {

									e.printStackTrace();
								}
								
							} catch (InstantiationException e) {

								e.printStackTrace();
							} catch (IllegalAccessException e) {

								e.printStackTrace();
							} catch (ClassNotFoundException e) {

								e.printStackTrace();
							}

						} catch (MalformedURLException e) {
							e.printStackTrace();
							System.out.println("jar url :"+jarclasstruepath+"/"+jarurl+" not exist!");
						}
						
					}
				}
				else
				{
					//this.setPlusReturnS("该插件使用方式不正确！");
					this.setPlusReturnS("");
				}
				
			}
			else
			{
				//this.setPlusReturnS("该插件不存在");
				this.setPlusReturnS("");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
     conn.close();
		}
		
	}
		
}
